package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YYGHException;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*==========================================
      时 间:   2022-03-07
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
        Department mongoDeparment = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        if(mongoDeparment == null){//添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
       }else{//更新
            department.setCreateTime(mongoDeparment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(mongoDeparment.getIsDeleted());
            department.setId(mongoDeparment.getId());
            departmentRepository.save(department);
        }
    }

    @Override
    public Page getDepartmentPage(Map<String, Object> paramMap) {
         int page=Integer.parseInt((String)paramMap.get("page"));
         int limit=Integer.parseInt((String)paramMap.get("limit"));
         String  hoscode =(String)paramMap.get("hoscode");


        Pageable pageable = PageRequest.of(page-1,limit, Sort.Direction.ASC,"createTime");

        Department department= new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        Example example=Example.of(department);
        return departmentRepository.findAll(example, pageable);
    }

    @Override
    public void remove(Map<String, Object> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
        Department department1 = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if(department1 == null){
           throw new YYGHException(20001,"没有该科室信息");
        }
        departmentRepository.deleteById(department1.getId());
    }


    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example=Example.of(department);
        //所有科室:
        List<Department> all = departmentRepository.findAll(example);

        //key:大科室编号, value:小科室列表
        Map<String, List<Department>> departmentMap = all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        List<DepartmentVo> departmentVoList = new ArrayList<DepartmentVo>();

        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            String bigCode = entry.getKey();
            List<Department> childDeparmentList = entry.getValue();

            //准备大科室数据
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(childDeparmentList.get(0).getBigname());

            List<DepartmentVo> resultChildDeparmentList = new ArrayList<DepartmentVo>();
            //准备小科室数据
            for (Department childDepartment : childDeparmentList) {
                DepartmentVo departmentVoNew=new DepartmentVo();
                departmentVoNew.setDepcode(childDepartment.getDepcode());
                departmentVoNew.setDepname(childDepartment.getDepname());
                resultChildDeparmentList.add(departmentVoNew);
            }
            departmentVo.setChildren(resultChildDeparmentList);
            departmentVoList.add(departmentVo);
        }

        return departmentVoList;
    }
}
